/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package domain.net;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 *
 * Used to create HTML pages from text that contains BB code.
 */
public class BBCodeParser
{
    /**
     * Convert the given input string to HTML.
     * @param textBBCode The text with BB code.
     * @return The HTML equivalent.
     */
    public static String toHTML(String textBBCode)
    {
        String html = textBBCode;

        Map<String, String> bbMap = new HashMap<String, String>();

        bbMap.put("(\r\n|\r|\n|\n\r)", "<br/>");
        bbMap.put("(?i)\\[b\\](?s)(.+?)\\[/b\\]", "<strong>$1</strong>");
        bbMap.put("(?i)\\[i\\](?s)(.+?)\\[/i\\]", "<span style='font-style:italic;'>$1</span>");
        bbMap.put("(?i)\\[u\\](?s)(.+?)\\[/u\\]", "<span style='text-decoration:underline;'>$1</span>");
        bbMap.put("(?i)\\[h1\\](?s)(.+?)\\[/h1\\]", "<h1>$1</h1>");
        bbMap.put("(?i)\\[h2\\](?s)(.+?)\\[/h2\\]", "<h2>$1</h2>");
        bbMap.put("(?i)\\[h3\\](?s)(.+?)\\[/h3\\]", "<h3>$1</h3>");
        bbMap.put("(?i)\\[h4\\](?s)(.+?)\\[/h4\\]", "<h4>$1</h4>");
        bbMap.put("(?i)\\[h5\\](?s)(.+?)\\[/h5\\]", "<h5>$1</h5>");
        bbMap.put("(?i)\\[h6\\](?s)(.+?)\\[/h6\\]", "<h6>$1</h6>");
        bbMap.put("(?i)\\[quote\\](?s)(.+?)\\[/quote\\]", "<blockquote>$1</blockquote>");
        bbMap.put("(?i)\\[code\\](?s)(.+?)\\[/code\\]", "<code>$1</code>");
        bbMap.put("(?i)\\[p\\](?s)(.+?)\\[/p\\]", "<p>$1</p>");
        bbMap.put("(?i)\\[p=(?s)(.+?),(?s)(.+?)\\](?s)(.+?)\\[/p\\]", "<p style='text-indent:$1px;line-height:$2%;'>$3</p>");
        bbMap.put("(?i)\\[center\\](?s)(.+?)\\[/center\\]", "<div align='center'>$1");
        bbMap.put("(?i)\\[align=(?s)(.+?)\\](?s)(.+?)\\[/align\\]", "<div align='$1'>$2");
        bbMap.put("(?i)\\[color=(?s)(.+?)\\](?s)(.+?)\\[/color\\]", "<span style='color:$1;'>$2</span>");
        bbMap.put("(?i)\\[size=(?s)(.+?)\\](?s)(.+?)\\[/size\\]", "<span style='font-size:$1;'>$2</span>");
        bbMap.put("(?i)\\[img\\](?s)(.+?)\\[/img\\]", "<img src='$1' />");
        bbMap.put("(?i)\\[img=(?s)(.+?),(?s)(.+?)\\](?s)(.+?)\\[/img\\]", "<img width='$1' height='$2' src='$3' />");
        bbMap.put("(?i)\\[email\\](?s)(.+?)\\[/email\\]", "<a href='mailto:$1'>$1</a>");
        bbMap.put("(?i)\\[email=(?s)(.+?)\\](?s)(.+?)\\[/email\\]", "<a href='mailto:$1'>$2</a>");
        bbMap.put("(?i)\\[url\\](?s)(.+?)\\[/url\\]", "<a href='$1'>$1</a>");
        bbMap.put("(?i)\\[url=(?s)(.+?)\\](?s)(.+?)\\[/url\\]", "<a href='$1'>$2</a>");
        bbMap.put("(?i)\\[youtube\\](?s)(.+?)\\[/youtube\\]", "<object width='640' height='380'><param name='movie' value='http://www.youtube.com/v/$1'></param><embed src='http://www.youtube.com/v/$1' type='application/x-shockwave-flash' width='640' height='380'></embed></object>");
        bbMap.put("(?i)\\[video\\](?s)(.+?)\\[/video\\]", "<video src='$1' />");

        for (Map.Entry entry : bbMap.entrySet())
        {
            html = html.replaceAll(entry.getKey().toString(), entry.getValue().toString());
        }

        return html;
    }
}